#include "net/Acceptor.h"
#include "eventloop/Eventloop.h"
#include "net/Sockaddr.h"

#include <stdio.h>
#include <unistd.h>
#include <optional>




void newConnection(int sockfd, const Sockaddr& peerAddr)
{
  printf("newConnection(): accepted a new connection from %s\n",
         peerAddr.IP_port().c_str());
  ::write(sockfd, "How are you?\n", 13);
  // ::close(sockfd);
}

int main()
{
  printf("main(): pid = %d\n", getpid());

  Sockaddr listenAddr(8888);
  Eventloop loop;

  Acceptor acceptor(loop, listenAddr, true);
  acceptor.set_new_connection_callback(
    [](std::unique_ptr<Socket> &&socket, const Sockaddr &peer){
        newConnection(socket->fd(), peer);
    });
  acceptor.listen();

  loop.loop();
}

